package com.ivt.controller.student;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ivt.core.util.Constants;
import com.ivt.model.Classes;
import com.ivt.model.Student;
import com.ivt.model.view.Rst;
import com.ivt.service.ClassesService;
import com.ivt.service.StudentService;
import com.ivt.utils.enums.AuditStatus;

@Controller("web-student-common-controller")
@RequestMapping("/common/student")
public class StudentAdviceController {

	@Resource
	private StudentService studentService;

	@Resource
	private ClassesService classesService;

	@RequestMapping("/{courseId}/sign/{teacherId}")
	public String sign(@PathVariable Long courseId,
			@PathVariable Long teacherId, ModelMap view) {

		List<Classes> classes = classesService
				.queryClassesByStatus(AuditStatus.PASS);
		view.addAttribute("classes", classes);

		view.addAttribute("courseId", courseId);
		view.addAttribute("teacherId", teacherId);

		return "student:login";
	}

	@RequestMapping("/register")
	@ResponseBody
	public Rst register(Student student, final HttpServletRequest request) {

		Rst rst = new Rst();
		student = studentService.create(student);
		if (0 < student.getId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
			rst.setData("/student");

			HttpSession session = request.getSession();

			session.setAttribute(Constants.SESSION_STUDENT_KEY, student);
		}

		return rst;
	}
}
